package com.tt.jshb.bill.detail;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzecool.core.base.TBaseActivity;
import com.hzecool.core.rxbus.RxBus;
import com.hzecool.widget.materialdialog.MaterialDialog;
import com.tt.jshb.R;
import com.tt.jshb.bean.DetailBean;
import com.tt.jshb.bean.FinishEvent;
import com.tt.jshb.bean.PayBean;
import com.tt.jshb.bean.RecycleHistoryBean;
import com.tt.jshb.bill.pay.QrCodePayActivity;
import com.tt.jshb.bill.zq.YQActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * @author tutu
 * @time 2018/7/26
 */

public class DetailActivity extends TBaseActivity<IDetailView, DetailPresenter>
        implements IDetailView {


    @BindView(R.id.tv_titel)
    TextView tvTitel;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_menu)
    TextView tvMenu;
    @BindView(R.id.title_root)
    RelativeLayout titleRoot;
    @BindView(R.id.tv_left_pay)
    TextView tvLeftPay;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.tv_4)
    TextView tv4;
    @BindView(R.id.tv_zq)
    TextView tvZq;
    @BindView(R.id.tv_hk)
    TextView tvHk;
    private DetailBean detailBean;

    @Override
    public void onLoadData(Object o) {

    }

    @Override
    public void onEmptyData() {

    }

    @Override
    public void onLoadError(String msg) {

    }

    @Override
    public void onNetError(String msg) {

    }

    @Override
    public int getLayoutID() {
        return R.layout.bill_detail;
    }

    @Override
    public void initView() {
        RecycleHistoryBean.DataBean dataBean = getIntent().getParcelableExtra("item");
        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvZq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailActivity.this, YQActivity.class);
                intent.putExtra("id", detailBean.getData().getId() + "");
                startActivity(intent);
            }
        });

        tvHk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                List<String> items = new ArrayList<>();
                items.add("支付宝");
                items.add("微信");

                new MaterialDialog.Builder(DetailActivity.this)
                        .title("选择展期期限")
                        .items(items)
                        .itemsCallbackSingleChoice(-1, (dialog, view, which, text) -> {
                            String fs = which == 0 ? "1" : "3";
                            mPresenter.payInfo(String.valueOf(detailBean.getData().getId()), fs);
                            return true;
                        })
                        .positiveText("确定")
                        .negativeText("取消")
                        .cancelable(false)
                        .show();
            }
        });

        mPresenter.getDetail(dataBean.getId() + "");

        RxBus.obtainEvent(FinishEvent.class)
                .subscribe(new Action1<FinishEvent>() {
                    @Override
                    public void call(FinishEvent finishEvent) {
                        if (finishEvent.getType()==1){
                            finish();
                        }
                    }
                });
    }


    @Override
    public void initTitle(ImageView ivBack, TextView tvBack, View llBack, TextView titleName, TextView tvMenu, View titleRoot) {
        titleName.setText("借款详情");
    }

    @Override
    protected DetailPresenter createPresenter() {
        return new DetailPresenter();
    }

    @Override
    public void onLoadDetail(DetailBean bean) {
        detailBean = bean;
        tvLeftPay.setText(bean.getData().getZe() + "元");
        tv1.setText(bean.getData().getZe() + "元");
        tv2.setText(bean.getData().getTs() + "天");
        tv3.setText(bean.getData().getJktimes() + "");
        tv4.setText(bean.getData().getEndtimes() + "");

    }

    @Override
    public void onPayResult(PayBean bean) {
        Intent intent = new Intent(this,QrCodePayActivity.class);
        intent.putExtra("payInfo",bean);
        startActivity(intent);
    }
}
